<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 自动程序接口
 */
class Crte extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 矿工产币
     * 
     */
    public function produceCoin(){
        //找一小时前已经点击签到的和加速中的矿工
        $where['type'] = ['in','2,4'];
        // $where['status'] = ['in','1,3'];
        $where['start_time1'] = ['<',(time()-60*60)];
        $list = db('user_miners')->where($where)->limit(40)->select();
        if(empty($list)){
        	$this->success('全部产币完成');
        }
       	//找到他们的矿工准备产币
        foreach($list as $k=>$v){
        	//矿机信息
        	$miners = db('miners')->where(['id'=>$v['miners_id']])->find();
        	//金币
        	$gold = round($miners['production_jin']/$miners['length'],2);
        	//银币
        	$silver = round($miners['production_yin']/$miners['length'],2);
        	//增加币
        	db('user')->where(['id'=>$v['user_id']])->setInc('gold',$gold);
        	db('user')->where(['id'=>$v['user_id']])->setInc('silver',$silver);
        	//加入财务日志
        	$this->addfinancial($v['user_id'],2,$miners['name'].'产币',5,$gold,$silver,0,$v['id']);
        	if($v['type']==2){
        		//签到产币的
        		$data['type'] = 3;	
        	}
        	if($v['type']==4){
        		//加速产币的
        		$data['type'] = 5;
        	}
        	//检测签到时间是不是今天
        	$today = date('d');
        	$day_c = date('d',$v['start_time1']);
        	if($today>$day_c){
        		//昨天签的到 或者加的速 今天清楚状态
        		$data['type'] = 1;
        	}
        	db('user_miners')->where(['id'=>$v['id']])->update($data);

        }
        $this->success('产币成功',$list);
    }
    /**
     * 检测是否到期
     */
    public function checktime(){
    	$where['status'] = 1;
    	$where['end_time'] = ['<',time()];
    	$data['status'] = 99;
    	$r = db('user_miners')->where($where)->update($data);
    	if($r){
    		$this->success('修改状态成功');
    	}else{
    		$this->success('暂无到期矿工');
    	}
    }
     /**
     * 加入财务日志
     */
    public function addfinancial($user_id,$type=1,$desc='',$status=1,$glod_num=0,$silver_num=0,$strength=0,$miners_id=0){
        $data['user_id'] = $user_id;
        $data['type'] = $type;
        $data['desc'] = $desc;
        $data['status'] = $status;
        $data['glod_num'] = $glod_num;
        $data['silver_num'] = $silver_num;
        $data['strength'] = $strength;
        $data['miners_id'] = $miners_id;
        $data['createtime'] = time();
        $r = db('user_log')->insert($data);
    }
    /**
     * 检测两小时没有付款的
     */
    public function checkstatus(){
        //调出所有两小时前已经匹配的单子
        $where['status'] =2;
        $where['matchtime'] = ['<',(time()-2*60*60)];
        $list = db('order')->where($where)->select();
        if(empty($list)){
            $this->success('暂无此类订单');
        }else{
            foreach($list as $k=>$v){
                //返回挂单原有值
                db('put_order')->where(['id'=>$v['put_id']])->setDec('out_num',$v['gold_num']);
                $data['status'] = 5;
                db('order')->where(['id'=>$v['id']])->update($data);
            }
            $this->success('操作成功',$list);
            
        }
    }

}
